// pages/Notice_detail/Notice_detail.js
let biaoti_flag = 0
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    message_list: '',
    _id: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      _id: options._id
    })
    let that = this;
    console.log("--------------这是", options._id)
    app.initCloudFunction().then(cloud => {
      cloud.callFunction({
        name: "getIndexInfo",
        data: {
          action: "notice",
          _id: this.data._id,
          // _id:"18ed0968619b4675069edff54b00fce0",
          sign: 19,
        }
      }, ).then(res => {
        console.log(res.result.data)
        let temp = res.result.data
        let last = new Date(temp.date)
        let string_last = new Date(last).getFullYear() + '年' + (new Date(last).getMonth() + 1) + '月' + new Date(last).getDate() + '日';
        temp.time = string_last;
        let example0 = "香港中文大学（深圳）龙岗附属学校（教育集团）2021年下半年赴武汉面向毕业生"
        let example1 = "香港中文大学（深圳）龙岗附属学校（教育集团）2021年下半年赴武汉面向毕业生公开招聘教师公告公告公告公告"
        if (temp.title.length >= example0.length && temp.title.length <= example1.length) {
          console.log("hhhh")
          biaoti_flag = 1
          that.setData({
            biaoti_flag: 1
          })
        } else {
          console.log("qqqq")
          biaoti_flag = 0
          that.setData({
            biaoti_flag: 0
          })
        }
        if (temp.fujian_name) {
          console.log("fujiNAME--------:", temp.fujian_name)
          let temp1 = temp.fujian_name.split('\n\n')
          let temp2 = []
          temp1.forEach(element => {
            temp2.push(element.replace(/(^\s*)|(\s*$)/g, ""))
          });
          temp.fujian_name = temp2
          console.log("qqq_temp:", temp)
        }
        var length = temp.texts.length
        var contents = []
        console.log("temp:", temp)
        for (let i = 0; i < length; i++) {
          let text = temp.texts[i]
          let picture = temp.pictures[i]
          if (picture == undefined) {
            picture = ""
          }
          contents.push([text, picture])
        }
        console.log("==========", contents)
        that.setData({
          message_list: [temp],
          contents: contents
        })
      })
    })
  },
  top_preview(options) {
    const that = this
    var current = options.target.dataset.url
    wx.previewImage({
      urls: that.data.message_list[0].pictures,
      current: current
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  download: function (e) {
    console.log("---------下载--------")
    //获取传递的链接id
    // console.log("e:",e)
    var index = e.currentTarget.id
    console.log("hhhhh", index)
    //获取下载链接
    let url = this.data.message_list[0].files[index]
    console.log("url:!!!", url)

    let fileName = this.data.message_list[0].fujian_name[index];
    console.log("fujianname", fileName)
    wx.downloadFile({
      url: url,
      success: function (res) {
        console.log(res);
        const manage = wx.getFileSystemManager();
        if (res.statusCode === 200) {
          console.log("temp:", res.tempFilePath)
          var x = res.tempFilePath.split(".")
          console.log("file:", x[x.length - 1])
          fileName = fileName + "." + x[x.length - 1]
          manage.saveFile({
            tempFilePath: res.tempFilePath,
            filePath: wx.env.USER_DATA_PATH + "/" + fileName,
            success: function (res) {
              console.log("保存成功！", res)

              // 打开文档
              wx.openDocument({
                filePath: wx.env.USER_DATA_PATH + "/" + fileName,
                showMenu: true,
                success: function (res) {
                  console.log(wx.env.USER_DATA_PATH + '/' + fileName)
                  console.log('打开文档成功:', res)
                },
                fail: function (res) {
                  console.log(wx.env.USER_DATA_PATH + '/' + fileName)
                  console.log('打开失败', res);
                }
              })
            },
            fail: function (res) {
              console.log("打开失败", res)
            }
          });

        }
      },
      fail: function (v) {
        console.log("失败的原因是", v)
      }
    })
    // wx.downloadFile({
    //   url: url,
    //   success: function(res) {
    //     console.log("saasaa",res);
    //     const manage = wx.getFileSystemManager();
    //     if (res.statusCode === 200) {
    //       console.log("temp:",res.tempFilePath)
    //       var x=res.tempFilePath.split(".")
    //       console.log("file:",x[x.length-1])
    //       fileName=fileName+"."+x[x.length-1]
    //        manage.saveFile({
    //         tempFilePath: res.tempFilePath,
    //         filePath: wx.env.USER_DATA_PATH + "/" + fileName,
    //         success: function(res) {
    //           console.log("保存成功！",res)

    //           // 打开文档
    //       wx.openDocument({
    //         filePath: wx.env.USER_DATA_PATH + "/" + fileName,
    //         showMenu:true,
    //         success: function(res) {
    //           console.log(wx.env.USER_DATA_PATH+'/'+fileName)
    //           console.log('打开文档成功:',res)
    //         },
    //         fail: function(res) {
    //           console.log(wx.env.USER_DATA_PATH+'/'+fileName)
    //           console.log('打开失败',res);
    //         }
    //       })
    //         },
    //         fail:function(res){
    //           console.log("打开失败",res)
    //         }
    //       });

    //     }
    //   }
    // })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }

})